<?php

namespace Admin\Controller;

use Think\Controller;

class UsersController extends CommonController {

    public function index() {
        $nickname = I("nickname");
        if (!empty($nickname)) {
            $map['nickname'] = array("like", "%" . $nickname . "%");
        }
        $map['m.status'] = array('egt', 0);

        $total = D("Member")->alias('m')->field("m.*,um.status as ustauts")->join('__UCENTER_MEMBER__ um ON m.uid= um.id')->where($map)->count();
        $page = new \Think\Page($total, 10, $REQUEST);
        // if($total>$listRows){
        $page->setConfig('theme', ' %UP_PAGE% %LINK_PAGE% %DOWN_PAGE%  %HEADER%');
        // }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');
        $this->assign('_total', $total);


        $list = D("Member")->alias('m')->field("m.*,um.status as ustauts,um.username as username")->join('__UCENTER_MEMBER__ um ON m.uid= um.id')->where($map)->limit($page->firstRow . ',' . $page->listRows)->order("m.status desc")->select();



        $this->assign('_list', $list);
        $this->meta_title = '用户列表';
        $this->display();
    }

    /**
     * 修改用户状态
     */
    public function changeStatus($id) {
        $method = I("get.method");

        switch ($method) {
            case "forbidUser": //禁止用户
                $this->forbid("Member", array("uid" => $id));
                break;
            case "resumeUser" :  //启用用户
                $this->resume("Member", array("uid" => $id));
                break;
            case "deleteUser" :  //删除用户
                $this->delete("Member", array("uid" => $id));
                break;
        }
    }

    /**
     * 用户添加
     */
    public function add() {


        if (IS_POST) {
            //先加到ucenterMember中去
            if (D("UcenterMember")->create()) {
                $newdata = array(
                    //"uid"=>$addid,
                    "nickname" => I("nickname"),
                    "sex" => I("sex"),
                    "birthday" => I("birthday"),
                    "qq" => I("qq"),
                );
                if (D("Member")->create($newdata)) {

                    if ($addid = D("UcenterMember")->add()) {

                        $newdata = array(
                            "uid" => $addid,
                            "nickname" => I("nickname"),
                            "sex" => I("sex"),
                            "birthday" => I("birthday"),
                            "qq" => I("qq"),
                            "logo" => I("logo"),
                        );
                        if (D("Member")->create($newdata)) {
                            if (D("Member")->add()) {
                                $this->success("添加成功");
                            } else {
                                $this->error("添加失败");
                            }
                        }
                    }
                } else {

                    $this->error(D("Member")->getError());
                }
            } else {
                $this->error(D("UcenterMember")->getError());
            }
        }
        $this->meta_title = '添加新用户';
        $this->display("edit");
    }

    /**
     * 用户编辑
     */
    public function edit($uid) {
        if (IS_POST) {
            $password = I("password");
            if (empty($password)) {
                unset($_POST["password"]);
                unset($_POST["repassword"]);
            }
            if (D("UcenterMember")->create()) {
                if ($addid = D("UcenterMember")->save()) {
                    if (D("Member")->create()) {
                        if (D("Member")->save()) {
                            $this->success("保存成功");
                        } else {
                            $this->error("保存失败");
                        }
                    } else {
                        $this->error(D("Member")->getError());
                    }
                }
            } else {
                $this->error(D("UcenterMember")->getError());
            }
        }
        $map["m.uid"] = $uid;
        $info = D("Member")->alias('m')->join('__UCENTER_MEMBER__ um ON m.uid= um.id')->where($map)->find();

        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 个人信息页
     */
    public function profile() {
        if (IS_POST) {
            if (D("Member")->create()) {
                if (D("Member")->save()) {
                    
                     $auth = array(
            'uid'             => UID,
            'username'        => I("nickname"),
            'last_login_time' => session("lqwsys_auth")["last_login_time"],
            'logo'=>I("logo"),
            
        );

        session('lqwsys_auth', $auth);
        session('lqwsys_auth_sign', data_auth_sign($auth));
                    $this->success("保存成功");
                } else {
                    $this->error("保存失败");
                }
            } else {
                $this->error(D("Member")->getError());
            }
        }
        $map["m.uid"] = UID;
        $info = D("Member")->alias('m')->join('__UCENTER_MEMBER__ um ON m.uid= um.id')->where($map)->find();
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 修改用户密码
     */
    public function updatepass() {
       
        if(IS_POST){
              $map["m.uid"] = UID;
             $info = D("Member")->alias('m')->join('__UCENTER_MEMBER__ um ON m.uid= um.id')->where($map)->find();
             $oldpassword=I("oldpassword");
             $password=I("password");
             $repassword=I("repassword");
             if(empty($password)){
                 $this->error("密码不能为空");
             }
             if($password!=$repassword){
                 $this->error("两次密码不一致");
             }
             $tp_password=think_ucenter_md5($oldpassword, C("DATA_AUTH_KEY"));
        
             if($info["password"]==$tp_password){
               $newpassword=think_ucenter_md5($password,C("DATA_AUTH_KEY"));
                 if( D("UcenterMember")->where("id=".UID)->save(array("password"=>$newpassword))){
                     $this->success("密码成功");
                 }else{
                   
                     $this->error("修改失败");
                 }
                 
             }else{
                 $this->error("旧密码错误");
             }
            
        }
        $this->display();
    }

}
